﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input.Touch;
using Microsoft.Xna.Framework.Media;

namespace Elementum
{
    class Stage1 : Stage
    {


        public Stage1(Game game) : base(game) { }
        //MicSound mic;
        int test = 0;
        public override void InitializeScenes()
        {
            base.InitializeScenes();

            Transition tra = new DefauldTransition(1, 0, 0, 60); 
            tra.Register(this);
            Animation title = new Animation("images/title/stageTitle", new Rectangle(2, 368, 132, 42), new Point(132, 42));
            title.Scale = 1.2f;
            tra = new TitleTransition(title, new Vector2(430, 150), 0, 1, 80, 220);//460, 391
            tra.Register(this);

            for (int i = 0; i < 4; i++)
            {
                Animation a = new Animation("images/stages/stage1/m" + i);
                a.LayerDepth = 0.9f;
                _backGround.Add(a);
            }
            _backGroundPos = new Vector2(-1000, -500);


            for (int i = 0; i < 4; i++)
            {
                Animation wallBack = new Animation("images/stages/stage1/stage1_" + i);
                //wallBack.LayerDepth = 0.9f;
                _wallBackGround.Add(wallBack);
            }
            _wallBackGroungPos = new Vector2(0, 52);

            
            //_scenceBound = new Rectangle((int)(basePos.X + 380), (int)(basePos.Y + 480),
            //       1024 * (_backGround.Count() - 1) + _backGround[_backGround.Count() - 1]._frameSize.X - 770,
            //      _backGround[0]._frameSize.Y - 620);
            _scenceBound = new Rectangle(430, 20,
                   2540,
                  550);

            camera.Bound = _scenceBound;

            //mic = new MicSound(60);


            GroundObject grount;
            GroundObject underBund;

            GameDoor door;
            GameKey key;

            ProtalGate protalSwitch;
            

            protalSwitch = new ProtalGate("protal0", _world, this); protalSwitch.ProtalCommand = new ProtalCommand("rootStage", "rootProtal3");protalSwitch.Position = new Vector2(460, 391); Actors.Add(protalSwitch);

            grount = new GroundObject(this._world, this); grount.Position = new Vector2(448, 466); grount.CurrentAnimation = new Animation("images/ground/dirtwall", new Point(824, 50)); grount.Alpha = 0f; grount.Size = new Vector2(824, 50); grount.Rotation = 0f; Actors.Add(grount);
            grount = new GroundObject(this._world, this); grount.Position = new Vector2(30, 244); grount.CurrentAnimation = new Animation("images/ground/dirtwall", new Point(12, 424)); grount.Alpha = 1f; grount.Size = new Vector2(12, 424); grount.Rotation = 0f; Actors.Add(grount);
            grount = new GroundObject(this._world, this); grount.Position = new Vector2(990, 413); grount.CurrentAnimation = new Animation("images/ground/dirtwall", new Point(294, 50)); grount.Alpha = 0f; grount.Size = new Vector2(294, 50); grount.Rotation = 0.4f; Actors.Add(grount);
            grount = new GroundObject(this._world, this); grount.Position = new Vector2(1254, 358); grount.CurrentAnimation = new Animation("images/ground/dirtwall", new Point(276, 50)); grount.Alpha = 0f; grount.Size = new Vector2(276, 50); grount.Rotation = 0f; Actors.Add(grount);
            grount = new GroundObject(this._world, this); grount.Position = new Vector2(1521, 273); grount.CurrentAnimation = new Animation("images/ground/grass", new Point(100, 12)); grount.Alpha = 0.8f; grount.Size = new Vector2(100, 12); grount.Rotation = 0f; Actors.Add(grount);
            grount = new GroundObject(this._world, this); grount.Position = new Vector2(721, 381); grount.CurrentAnimation = new Animation("images/ground/grass", new Point(100, 12)); grount.Alpha = 0.7f; grount.Size = new Vector2(100, 12); grount.Rotation = 0f; Actors.Add(grount);
            grount = new GroundObject(this._world, this); grount.Position = new Vector2(1476, 414); grount.CurrentAnimation = new Animation("images/ground/dirtwall", new Point(228, 50)); grount.Alpha = 0f; grount.Size = new Vector2(228, 50); grount.Rotation = -0.5500001f; Actors.Add(grount);
            grount = new GroundObject(this._world, this); grount.Position = new Vector2(1731, 475); grount.CurrentAnimation = new Animation("images/ground/dirtwall", new Point(302, 50)); grount.Alpha = 0f; grount.Size = new Vector2(302, 50); grount.Rotation = 0f; Actors.Add(grount);
            grount = new GroundObject(this._world, this); grount.Position = new Vector2(1677, 216); grount.CurrentAnimation = new Animation("images/ground/grass", new Point(100, 12)); grount.Alpha = 0.6f; grount.Size = new Vector2(100, 12); grount.Rotation = 0f; Actors.Add(grount);
            grount = new GroundObject(this._world, this); grount.Position = new Vector2(1837, 167); grount.CurrentAnimation = new Animation("images/ground/grass", new Point(100, 12)); grount.Alpha = 0.5f; grount.Size = new Vector2(100, 12); grount.Rotation = 0f; Actors.Add(grount);
            grount = new GroundObject(this._world, this); grount.Position = new Vector2(1992, 122); grount.CurrentAnimation = new Animation("images/ground/grass", new Point(100, 12)); grount.Alpha = 0.4f; grount.Size = new Vector2(100, 12); grount.Rotation = 0f; Actors.Add(grount);
            grount = new GroundObject(this._world, this); grount.Position = new Vector2(1983, 460); grount.CurrentAnimation = new Animation("images/ground/dirtwall", new Point(202, 50)); grount.Alpha = 0f; grount.Size = new Vector2(202, 50); grount.Rotation = 0.15f; Actors.Add(grount);
            grount = new GroundObject(this._world, this); grount.Position = new Vector2(2143, 445); grount.CurrentAnimation = new Animation("images/ground/dirtwall", new Point(128, 50)); grount.Alpha = 0f; grount.Size = new Vector2(128, 50); grount.Rotation = 0f; Actors.Add(grount);
            grount = new GroundObject(this._world, this); grount.Position = new Vector2(2148, 179); grount.CurrentAnimation = new Animation("images/ground/grass", new Point(100, 12)); grount.Alpha = 0.6f; grount.Size = new Vector2(100, 12); grount.Rotation = 0f; Actors.Add(grount);
            grount = new GroundObject(this._world, this); grount.Position = new Vector2(2303, 123); grount.CurrentAnimation = new Animation("images/ground/grass", new Point(100, 12)); grount.Alpha = 0.2f; grount.Size = new Vector2(100, 12); grount.Rotation = 0f; Actors.Add(grount);
            grount = new GroundObject(this._world, this); grount.Position = new Vector2(2353, 393); grount.CurrentAnimation = new Animation("images/ground/dirtwall", new Point(300, 50)); grount.Alpha = 0f; grount.Size = new Vector2(300, 50); grount.Rotation = 0.35f; Actors.Add(grount);
            grount = new GroundObject(this._world, this); grount.Position = new Vector2(2635, 343); grount.CurrentAnimation = new Animation("images/ground/dirtwall", new Point(296, 50)); grount.Alpha = 0f; grount.Size = new Vector2(296, 50); grount.Rotation = 0f; Actors.Add(grount);
            key = new GameKey("New_Key_S1", _world, this); key.Position = new Vector2(2336, 99); key.Size = new Vector2(23, 35); key.KeyID = "New_Door_1"; Actors.Add(key);
            door = new GameDoor("New_Door_S1", _world, this); door.Position = new Vector2(2573, 266); door.Size = new Vector2(60, 100); door.DoorID = "New_Door_1"; Actors.Add(door);
            grount = new GroundObject(this._world, this); grount.Position = new Vector2(3111, 343); grount.CurrentAnimation = new Animation("images/ground/dirtwall", new Point(515, 50)); grount.Alpha = 0f; grount.Size = new Vector2(515, 50); grount.Rotation = 0f; Actors.Add(grount);
            underBund = new GroundObject(this._world, this); underBund.Position = new Vector2(2816, 450); underBund.CurrentAnimation = new Animation("images/ground/thorn", new Point(68, 64)); underBund.Size = new Vector2(68, 64); underBund.BodyData = "killBund"; underBund.Rotation = 0f; Actors.Add(underBund);
            underBund = new GroundObject(this._world, this); underBund.Position = new Vector2(2816, 498); underBund.CurrentAnimation = new Animation("images/ground/thorn", new Point(68, 64)); underBund.Size = new Vector2(68, 64); underBund.BodyData = "killBund"; underBund.Rotation = 0f; Actors.Add(underBund);
            
            protalSwitch = new ProtalGate("Stage1_Protal_1", _world, this); protalSwitch.ProtalCommand = new ProtalCommand("stage1_1", "Stage1_1_Portal_1"); protalSwitch.Position = new Vector2(2938, 268); Actors.Add(protalSwitch);
            grount = new GroundObject(this._world, this); grount.Position = new Vector2(3325, 151); grount.CurrentAnimation = new Animation("images/ground/dirtwall", new Point(14, 334)); grount.Size = new Vector2(14, 334); grount.Alpha = 0f; grount.Rotation = 0f; Actors.Add(grount);

            _startProtal = new ProtalCommand("stage1", "protal0");

            Font = ResourceManager.LoadSpriteFont("SpriteFont1");
            MusicManager.AddMusic("Music/doorlock", "doorlock", 0.001f,false);

            _song = MusicManager.LoadSong("Music/stageroot");
            
            if (GameClass._musicString == "Music/stageroot")
            { }
            else
            {
                GameClass._musicString = "Music/stageroot";
                MediaPlayer.Play(_song);
            }
            MediaPlayer.IsRepeating = true;
           // MediaPlayer.Volume = 1f;

        }

        public override void Restart()
        {
            foreach (VisibleObject b in actors)
            {
                b.Restart();
            }
            StageManager.ProtalCommand = _startProtal;
        }


        public override void OnDraw(SpriteBatch spriteBatch, int delta)
        {
            base.OnDraw(spriteBatch, delta);
            //GraphicsDevice.Clear(Color.White);


            spriteBatch.Begin(SpriteSortMode.BackToFront, BlendState.AlphaBlend, null, null, null, null, camera.Transform);

            foreach (VisibleObject b in actors)
                b.OnDraw(spriteBatch, camera.Transform);

            spriteBatch.End();

            //spriteBatch.Begin();
           // spriteBatch.DrawString(Font, "" + test, new Vector2(50, 50), Color.Black);
           // spriteBatch.End();

        }

        public override void Update(int delta)
        {
            base.Update(delta);

            //a.Update(delta);
            //mic.Update();
            // MusicManager.FadeInMusic("first");


        }

        public override Stage Clone()
        {
            Stage1 stage = new Stage1(Game);
            stage.InitializeScenes();
            return stage;
        }

        public override void Dispose()
        {
            //mic.Dispose();
            base.Dispose();
            //mic.Dispose();
        }
    }
}
